package com.lhq.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhq.business.domain.Chapter;
import com.lhq.business.service.IChapterService;
import com.lhq.common.annotation.Anonymous;
import com.lhq.common.annotation.Log;
import com.lhq.common.core.controller.BaseController;
import com.lhq.common.core.domain.AjaxResult;
import com.lhq.common.enums.BusinessType;
import com.lhq.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 章节Controller
 *
 * @author lhq
 * @date 2024-05-11
 */
@RestController
@RequestMapping("/business/chapter")
public class ChapterController extends BaseController
{
    @Autowired
    private IChapterService chapterService;

    /**
     * 查询章节列表
     */
    @PreAuthorize("@ss.hasPermi('business:chapter:list')")
    @GetMapping("/list")
    public AjaxResult list(Chapter chapter,
                             @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<Chapter> page =new Page<>(pageNum,pageSize);
        return AjaxResult.successPage(chapterService.selectList(page,chapter));
    }

    /**
     * 导出章节列表
     */
    @PreAuthorize("@ss.hasPermi('business:chapter:export')")
    @Log(title = "章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Chapter chapter,
                       @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<Chapter> page =new Page<>(pageNum,pageSize);
        List<Chapter> list = chapterService.selectList(page,chapter).getRecords();
        ExcelUtil<Chapter> util = new ExcelUtil<Chapter>(Chapter.class);
        util.exportExcel(response, list, "章节数据");
    }

    /**
     * 获取章节详细信息
     */
    @Anonymous
    // @PreAuthorize("@ss.hasPermi('business:chapter:query')")
    @GetMapping(value = "/{bookId}/{ChapterId}")
    public AjaxResult getChapterInfo(@PathVariable("bookId") String bookId,
                                     @PathVariable("ChapterId") String ChapterId)
    {
        return success(chapterService.getChapterInfo(bookId, ChapterId));
    }

    /**
     * 新增章节
     */
    @PreAuthorize("@ss.hasPermi('business:chapter:add')")
    @Log(title = "章节", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Chapter chapter)
    {
        return toAjax(chapterService.insertChapter(chapter));
    }

    /**
     * 修改章节
     */
    @PreAuthorize("@ss.hasPermi('business:chapter:edit')")
    @Log(title = "章节", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Chapter chapter)
    {
        return toAjax(chapterService.updateChapter(chapter));
    }

    /**
     * 删除章节
     */
    @PreAuthorize("@ss.hasPermi('business:chapter:remove')")
    @Log(title = "章节", businessType = BusinessType.DELETE)
	@GetMapping("/del/{id}")
    public AjaxResult remove(@PathVariable("id") String  id)
    {
        return toAjax(chapterService.deleteById(id));
    }
}
